package com.lin.missyou.api.v1;

import com.lin.missyou.core.LocalUser;
import com.lin.missyou.core.UnifyResponse;
import com.lin.missyou.core.enumeration.CouponStatus;
import com.lin.missyou.core.interceptors.ScopeLevel;
import com.lin.missyou.exception.CreateSuccess;
import com.lin.missyou.exception.http.ParameterException;
import com.lin.missyou.model.Coupon;
import com.lin.missyou.service.CouponService;
import com.lin.missyou.vo.CouponCategoryVO;
import com.lin.missyou.vo.CouponPureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GetMapping("/by/category/{cid}")
    public List<CouponPureVO> getCouponListByCategory(
            @PathVariable Long cid) {
        List<Coupon> couponList = couponService.getByCategory(cid);
        if (couponList.isEmpty()) {
            return Collections.emptyList();
        }
        List<CouponPureVO> vos = CouponPureVO.getList(couponList);
        return vos;
    }


    @GetMapping("/whole_store")
    public List<CouponPureVO> getWholeStoreCouponList() {
        List<Coupon> coupons = this.couponService.getWholeStoreCoupons();
        if (coupons == null) {
            return Collections.emptyList();
        }
        return CouponPureVO.getList(coupons);
    }

    // 用户领取优惠卷
    @ScopeLevel
    @PostMapping("/collect/{id}")
    public void collectCoupon(@PathVariable Long id) {
        // 不能显示
        Long userId = LocalUser.getUser().getId();
        this.couponService.collectOneCoupon(userId, id);

        UnifyResponse.creatSuccess(0);
    }


    @ScopeLevel
    @GetMapping("/myself/by/status/{status}")
    public List<CouponPureVO> getMyCouponByStatus(@PathVariable Integer status) {
        Long uid = LocalUser.getUser().getId();
        List<Coupon> couponList;

        // 触发机制 时机  过期

        switch (CouponStatus.toType(status)) {
            case AVAILABLE:
                couponList = this.couponService.getMyAvailableCoupons(uid);
                break;
            case USED:
                couponList = this.couponService.getMyUsedCoupons(uid);
                break;
            case EXPIRED:
                couponList = this.couponService.getMyExpiredCoupons(uid);
                break;
            default:
                throw new ParameterException(40001);
        }
        return CouponPureVO.getList(couponList);
    }

    @ScopeLevel
    @GetMapping("/myself/available/with_category")
    public List<CouponCategoryVO> getUserCouponWithCategory() {
        Long uid = LocalUser.getUser().getId();

        List<Coupon> couponList = this.couponService.getMyAvailableCoupons(uid);
        if (couponList == null) {
            return Collections.emptyList();
        }
//        List<CouponCategoryVO> couponCategoryVOList = new ArrayList<>();
//        couponList.forEach(coupon -> {
//            CouponCategoryVO couponCategoryVO = new CouponCategoryVO(coupon);
//            couponCategoryVOList.add(couponCategoryVO);
//        });
//                return couponCategoryVOList;
//
//        return couponList.stream().map(coupon -> {
//            CouponCategoryVO couponCategoryVO = new CouponCategoryVO(coupon);
//            return couponCategoryVO;
//        }).collect(Collectors.toList());
        return couponList.stream().map(CouponCategoryVO::new).collect(Collectors.toList());
    }

}
